package demo.practice.leetcode_msjd;

public class Msjd0204 {

    public static void main(String[] args) {
        ListNode root= new ListNode(1);
        root.next= new ListNode(4);
        root.next.next=new ListNode(3);
        root.next.next.next= new ListNode(2);
        root.next.next.next.next= new ListNode(5);
        root.next.next.next.next.next= new ListNode(2);
        Msjd0204 msjd0204= new Msjd0204();
        msjd0204.partition(root,3);

    }

    public ListNode partition(ListNode head, int x) {
        ListNode current = head;
        ListNode befor = null;
        ListNode beforHead = null;
        ListNode behind = null;
        ListNode behindHead = null;

        while (current != null) {
            if (current.val < x) {
                //放前面
                if (befor == null) {
                    befor = new ListNode(current.val);
                    beforHead = befor;
                } else {
                    befor.next = new ListNode(current.val);
                    befor = befor.next;
                }

            } else {
                //放后面
                if (behind == null) {
                    behind = new ListNode(current.val);
                    behindHead = behind;
                } else {
                    behind.next = new ListNode(current.val);
                    behind = behind.next;
                }
            }
            current = current.next;
        }

        if (beforHead != null) {
            befor.next = beforHead;
            return beforHead;
        } else {
            return behindHead;
        }
    }


//    public  void add(ListNode head,int val){
//        head.next=new
//    }


}
